package com.mondo.financialwallet.presentation.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.example.common.ui.AbsNetworkStateActivity;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.entity.ProductDetailsEntity;
import com.mondo.financialwallet.presentation.model.HotProducts;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;

import butterknife.Bind;

/**
 * 产品详情
 *
 * TODO 有中文
 */
public class ProductDetailsActivity extends AbsNetworkStateActivity {

    private static final String EXTRA_PRODUCT = "PRODUCT";

    public static void launch(Context context, HotProducts products) {
        context.startActivity(new Intent(context, ProductDetailsActivity.class)
                .putExtra(EXTRA_PRODUCT, products));
    }

    @Bind(R.id.investment_view) View mInvestmentView;
    @Bind(R.id.expected_annualized_text) TextView mExpectedAnnualizedText;
    @Bind(R.id.investment_period_text) TextView mInvestmentPeriodText;
    @Bind(R.id.starting_amount_text) TextView mStartingAmountText;
    @Bind(R.id.product_type_text) TextView mProductTypeText;
    @Bind(R.id.publish_company_text) TextView mPublishCompanyText;
    @Bind(R.id.fund_direction_text) TextView mFundDirectionText;
    @Bind(R.id.investment_strategy_text) TextView mInvestmentStrategyText;
    @Bind(R.id.income_method_text) TextView mIncomeMethodText;
    @Bind(R.id.product_purchase_rate_text) TextView mProductPurchaseRateText;
    @Bind(R.id.product_management_rate_text) TextView mProductManagementRateText;
    @Bind(R.id.product_income_rule_text) TextView mProductIncomeRuleText;
    @Bind(R.id.product_redemption_time_text) TextView mProductRedemptionTimeText;
    @Bind(R.id.product_redemption_rate_text) TextView mProductRedemptionRateText;
    @Bind(R.id.product_arrival_time_text) TextView mProductArrivalTimeText;
    @Bind(R.id.amount_text_view) TextView mAmountTextView;
    @Bind(R.id.service_protocol_view) View mServiceProtocolView;
    @Bind(R.id.investment_protocol_view) View mInvestmentProtocolView;
    @Bind(R.id.product_closed_time_text) TextView mProductClosedTimeTextView;

    private HotProducts product;

    @Override
    public int getTitleLayoutRes() {
        return R.layout.include_default_title_bar;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_product_details;
    }

    @Override
    protected void initActions() {
        product = (HotProducts) getIntent().getSerializableExtra(EXTRA_PRODUCT);
        ActivityTitleBarHelper.init(this, product.getTitle());

        // 请求产品详情
        requestProductDetails();
    }

    private void requestProductDetails() {
        loadingNetwork();
        DataRepository.get().productDetails(String.valueOf(product.getId()), result -> {
            if (result.isSuccess()) {
                successNetwork();
                displayProductDetailInView(result.getData().get(0));
            } else {
                errorNetwork(result.getMsg());
            }
        });
    }

    // TODO 中文
    private void displayProductDetailInView(ProductDetailsEntity.DataBean details) {
        // 价格
        mAmountTextView.setText("$" + product.getMinimumAmount());

        // 产品特点
        ProductDetailsEntity.DataBean.AdvantagesBean advantages = details.getAdvantages();
        mExpectedAnnualizedText.setText(product.getAnnualizedRate()); // 年化收益率
        mInvestmentPeriodText.setText(product.getMinimumDay() + "天"); // 投资期限
        mStartingAmountText.setText("$" + product.getMinimumAmount()); // 起投资金
        mProductTypeText.setText(details.getCates());  // 产品类型
        mPublishCompanyText.setText(details.getCompanys());// 发布公司
        mIncomeMethodText.setText("一次性分配本金及收益"); // 收益方式
        mFundDirectionText.setText(advantages.getFund_invest()); // 资金投向
        mInvestmentStrategyText.setText(advantages.getInvestment_strategy()); // 投资策略

        // 交易规则
        ProductDetailsEntity.DataBean.TradesBean trades = details.getTrades();
        mProductPurchaseRateText.setText("购买费率为 " + trades.getSubscribe_fee()); // 购买费率 [购买费率为 0]
        mProductManagementRateText.setText(trades.getManage_fee() + "% (每年)"); // 管理费率 [2%(每年)]
        mProductArrivalTimeText.setText(trades.getRedeem_time_desc());           // 赎回时间
        mProductRedemptionTimeText.setText(trades.getProfit_rule_desc()); // 收益规则
        mProductRedemptionRateText.setText(trades.getRedeem_fee()); // 赎回费率 [赎回费率为 0]
        mProductIncomeRuleText.setText(trades.getProfit_rule_desc()); // 收益规则
        mProductClosedTimeTextView.setText(trades.getClose_day() + "天"); // 封闭时间

        // 相关协议
        mServiceProtocolView.setOnClickListener(v ->
                WebViewActivity.serviceAgreement(this));
        mInvestmentProtocolView.setOnClickListener(v ->
                WebViewActivity.assetAgreement(this));

        // 购买
        mInvestmentView.setOnClickListener(v ->
                PurchaseProductActivity.launch(this, product));
    }

    @Override
    public void onAnewRequest() {
        requestProductDetails();
    }
}
